package com.bm.util;
import java.util.Date;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;
import redis.clients.jedis.Pipeline;
public class ListenerTrade extends JedisPubSub{
	private static Logger logger=LogManager.getLogger(ListenerTrade.class);
	@Override
	public void onMessage(String channel, String message) {
		long starttime=System.currentTimeMillis();
		Jedis jedis=RedisUtil.getJedis();
		  try {    
			    long aTime=System.currentTimeMillis();
			  	Pipeline pipeline= jedis.pipelined();
			    long bTime=System.currentTimeMillis();
			    //将数据缓存到redis(    lpush先进后出  rpush 先进先出  )
			    pipeline.rpush("Trade", message+"Time:"+new Date());
			    long cTime=System.currentTimeMillis();
			    System.out.println("List[添加后]:"+jedis.llen("transactionsifno"));
			    logger.error("【新建jedis连接时间】"+(bTime-starttime)+"【缓存到redis时间】"+(cTime-bTime)+"【一共花费时间】"+(cTime-starttime));
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				logger.error("【insert to redis】  insert fail:"+e1.toString());
				//异常邮件提醒
				//MailUtil.send("【insert to redis】  insert fail:", e1.toString());
			}finally{
					RedisUtil.returnResource(jedis);
			}
	     //此处我们可以取消订阅  
	     if(message.equalsIgnoreCase("quit")){  
	            this.unsubscribe(channel);  
	      }  
	     
	}

	@Override
	public void onPMessage(String pattern, String channel, String message) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSubscribe(String channel, int subscribedChannels) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onUnsubscribe(String channel, int subscribedChannels) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPUnsubscribe(String pattern, int subscribedChannels) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPSubscribe(String pattern, int subscribedChannels) {
		// TODO Auto-generated method stub

	}

}
